<template>
  <div class="login-container">
    <!-- 背景 -->
    <GradientBackground />

    <!-- 登录卡片 -->
    <el-card class="login-box">
      <template #header>
        <h2 class="login-title">问卷调查系统</h2>
      </template>

      <el-form
        ref="formRef"
        :model="loginForm"
        :rules="rules"
        @keyup.enter="handleLogin"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
            :prefix-icon="User"
            size="large"
          />
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            :prefix-icon="Lock"
            show-password
            size="large"
          />
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            :loading="loading"
            @click="handleLogin"
            class="login-button"
            size="large"
          >
            {{ loading ? "登录中..." : "登录" }}
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from "vue";
import { ElMessage } from "element-plus";
import { useRouter } from "vue-router";
import request from "../utils/request.js";
import { User, Lock } from "@element-plus/icons-vue";

// 引入背景
import GradientBackground from "@/components/GradientBackground.vue";

const rules = {
  username: [
    { required: true, message: "请输入用户名", trigger: "blur" },
    {
      validator: (rule, value, callback) => {
        const invalidPattern = /[\\'"\;=<>!@#$%^&*(){}\[\]\s]/;
        if (invalidPattern.test(value)) {
          return callback(new Error("用户名不能包含特殊字符，如引号、分号、空格等"));
        }
        callback();
      },
      trigger: "blur",
    },
  ],
  password: [
    {
      validator: (rule, value, callback) => {
        if (!value) {
          return callback(new Error("请输入密码"));
        }
        if (value.length < 8 || value.length > 20) {
          return callback(new Error("密码长度必须在8到20位之间"));
        }
        if (!/[A-Z]/.test(value)) {
          return callback(new Error("密码必须包含至少一个大写字母"));
        }
        if (!/[a-z]/.test(value)) {
          return callback(new Error("密码必须包含至少一个小写字母"));
        }
        callback();
      },
      trigger: "blur",
    },
  ],
};

const router = useRouter();

const loginForm = reactive({
  username: "",
  password: "",
});

const loading = ref(false);
const formRef = ref(null);

const handleLogin = async () => {
  formRef.value.validate(async (valid) => {
    if (!valid) {
      ElMessage.error("请检查表单输入");
      return;
    }
    try {
      loading.value = true;

      const response = await request.post("/user/login", {
        name: loginForm.username,
        password: loginForm.password,
      });

      if (response.data.code === 200) {
        localStorage.setItem("token", response.data.data.token);
        localStorage.setItem("adminId", response.data.data.id);
        localStorage.setItem("name", response.data.data.name);

        ElMessage.success(response.data.message);
        router.push("/HomePage");
      } else {
        ElMessage.error(response.data.message);
      }
    } catch (error) {
      ElMessage.error(error);
    } finally {
      loading.value = false;
    }
  });
};
</script>

<style scoped>
.login-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;

  /* 移除原来的动画背景，因为现在用 SVG 做背景 */
  position: relative;
  overflow: hidden;
}

/* 登录卡片 */
.login-box {
  width: 420px;
  border-radius: 16px;
  padding: 10px 20px 30px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
  backdrop-filter: blur(10px);
  background: rgba(255, 255, 255, 0.9);
  animation: fadeIn 0.6s ease-out;
  z-index: 10; /* 确保在背景上层 */
}

.login-title {
  text-align: center;
  color: #303133;
  font-size: 26px;
  font-weight: 600;
  margin: 0;
}

/* 输入框间距 */
.el-form-item {
  margin-bottom: 28px;
}

/* 登录按钮 */
.login-button {
  width: 100%;
  height: 42px;
  font-size: 16px;
  border-radius: 8px;
  transition: all 0.25s;
}

.login-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(64, 158, 255, 0.4);
}

/* 淡入动画 */
@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(12px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
</style>
